package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;


import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration){
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        List<Object> list = new SimpleExecutor().query(configuration, mappedStatement, params);
        return (List<E>) list;
    }

    @Override
    public void insertObject(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException, SQLException {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        Integer result = new SimpleExecutor().executeUpdate(configuration, mappedStatement, params);
        if(result > 0){
            System.out.println("新增数据成功..");
        }else {
            new RuntimeException("新增数据失败..");
        }
    }

    @Override
    public void updateObject(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        Integer result = new SimpleExecutor().executeUpdate(configuration, mappedStatement, params);
        if(result > 0){
            System.out.println("修改数据成功..");
        }else {
            new RuntimeException("修改数据失败..");
        }
    }


    @Override
    public void delteObject(String statementid, Object... params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementid);
        Integer result = new SimpleExecutor().executeUpdate(configuration, mappedStatement, params);
        if(result > 0){
            System.out.println("删除数据成功..");
        }else {
            new RuntimeException("删除数据失败..");
        }
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        // 使用JDK动态代理，来为Dao接口生成代理对象
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 底层还是执行JDBC代码
                // 根据不同情况可以调用不同方法
                // 准备参数
                // 1.statementid:sql语句的唯一标识  namespace.id=接口全限定名.方法名
                // 方法名
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;

                // 2.参数设置 params:args
                if(methodName == "showUser"){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }else if(methodName == "addUser"){
                    insertObject(statementId, args);
                }else if(methodName == "updateUser"){
                    updateObject(statementId, args);
                }else if(methodName == "deleteUser"){
                    delteObject(statementId, args);
                }
                return null;
            }
        });
        return (T) proxyInstance;
    }
}
